package oneD.dynamics;

import iceb.plotutils.DynamicDataset;
import iceb.plotutils.ui.PlotPanel;
import inf.v3d.view.Viewer;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.swing.JFrame;

public class FunctionPlotter1D extends JFrame {
	/**
	 * class to draw a single 2d plot
	 */

	private static final long serialVersionUID = 2L;
	private PlotPanel plotPanel_;
	Map<String, DynamicDataset> plotDataset_;

	public FunctionPlotter1D() {
		// super();
		this.plotPanel_ = new PlotPanel();
		this.plotDataset_ = new HashMap<String, DynamicDataset>();
		this.setSize(600, 400);
	}

	public void plot(Viewer v) {
		Iterator it = this.plotDataset_.entrySet().iterator();

		while (it.hasNext()) {
			Map.Entry m = (Map.Entry) it.next();
			DynamicDataset d = (DynamicDataset) m.getValue();
			this.plotPanel_.addDataset(d);
		}

		v.add(plotPanel_);
		v.setDefaultCloseOperation(EXIT_ON_CLOSE);
		v.pack();
	}

	public boolean registerPlotName(String name) {
		if (this.plotDataset_.containsKey(name))
			return true;
		else {
			DynamicDataset dataset = new DynamicDataset();
			this.plotDataset_.put(name, dataset);
			return false;
		}
	}

	public void setXTitle(String title) {
		this.plotPanel_.setXLabel(title);
	}

	public void setYTitle(String title) {
		this.plotPanel_.setYLabel(title);
	}

	public boolean insertData(double x, double y, String plotName) {
		if (this.plotDataset_.containsKey(plotName)) {
			this.plotDataset_.get(plotName).addX(x);
			this.plotDataset_.get(plotName).addY(plotName, y);
			return true;
		} else {
			System.out.println("The plot " + plotName + " is not registered");
			return false;
		}
	}

}
